package com.livepan.business.controller;

import com.livepan.server.BaseController;
import com.livepan.server.error.BusinessException;
import com.livepan.server.response.CommonReturnType;
import com.livepan.server.service.CategoryService;
import com.livepan.server.service.model.CategoryModel;
import com.livepan.server.service.model.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    public static final String BUSINESS_NAME = "分类";

    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private CategoryService categoryService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonReturnType category(PageModel pageModel) {
        LOG.info("page:" + pageModel.getPage());
        categoryService.list(pageModel);
        return CommonReturnType.create(pageModel);
    }

    @ResponseBody
    @RequestMapping("/all")
    public CommonReturnType all() {
        return CommonReturnType.create(categoryService.all());
    }

    @ResponseBody
    @RequestMapping("/save")
    public CommonReturnType saveCategory(@RequestBody CategoryModel categoryModel) throws BusinessException {
        categoryService.save(categoryModel);
        return CommonReturnType.create(categoryModel);
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public CommonReturnType delete(@PathVariable String id) {
        categoryService.delete(id);
        return CommonReturnType.create(null);
    }
}
